<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>位置选择小游戏</title>
<style>
  body {
    background-color: #f0f4f8;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    margin: 0; padding: 0;
    font-family: "Microsoft YaHei", "SimHei", "SimSun", sans-serif;
  }
  h1 {
    margin-bottom: 2rem;
    color: #333;
    font-size: 1.8rem;
  }
  .mahjong-container {
    display: flex;
    gap: 1.5rem;
    flex-wrap: wrap;
    justify-content: center;
    padding: 0 1rem;
  }
  .mahjong {
    width: 80px;
    height: 120px;
    perspective: 1000px;
  }
  .card {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.6s;
    cursor: pointer;
    user-select: none;
  }
  .card-inner {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.6s;
  }
  .card.flipped .card-inner {
    transform: rotateY(180deg);
  }
  .front, .back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 8px;
    border: 2px solid #004d00;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: bold;
  }
  .front {
    background-color: white;
    color: #222;
    transform: rotateY(180deg);
    border-color: #999;
  }
  .back {
    background-color: #006400;
    background-image:
      repeating-linear-gradient(
        45deg,
        rgba(255, 255, 255, 0.05),
        rgba(255, 255, 255, 0.05) 2px,
        transparent 2px,
        transparent 6px
      ),
      repeating-linear-gradient(
        -45deg,
        rgba(255, 255, 255, 0.05),
        rgba(255, 255, 255, 0.05) 2px,
        transparent 2px,
        transparent 6px
      );
    box-shadow: inset 0 0 8px #004000;
    border-color: #004d00;
  }
  .card.disabled {
    cursor: default;
    pointer-events: none;
    opacity: 0.6;
  }
  @media (max-width: 480px) {
    .mahjong {
      width: 70px;
      height: 100px;
    }
  }
  button#resetBtn {
    margin-top: 2rem;
    padding: 10px 20px;
    font-size: 1.2rem;
    border-radius: 6px;
    border: none;
    background-color: #4caf50;
    color: white;
    cursor: pointer;
    user-select: none;
  }
  button#resetBtn:hover:not(:disabled) {
    background-color: #45a049;
  }
  button#resetBtn:disabled {
    background-color: #9e9e9e;
    cursor: default;
  }
</style>
</head>
<body>

<h1>位置选择小游戏</h1>

<div class="mahjong-container">
  <div class="mahjong">
    <div class="card" data-id="1">
      <div class="card-inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
  </div>
  <div class="mahjong">
    <div class="card" data-id="2">
      <div class="card-inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
  </div>
  <div class="mahjong">
    <div class="card" data-id="3">
      <div class="card-inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
  </div>
  <div class="mahjong">
    <div class="card" data-id="4">
      <div class="card-inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
  </div>
</div>

<button id="resetBtn" disabled>重新开始</button>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  $(function(){
    const directionsMaster = ['东', '南', '西', '北'];
    let directions = [...directionsMaster];

    function resetGame(){
      directions = [...directionsMaster];
      $('.card')
        .removeClass('flipped disabled')
        .find('.front').text('');
      $('#resetBtn').prop('disabled', true);
    }

    $('.card').on('click', function(){
      if ($(this).hasClass('disabled')) return;
      if (directions.length === 0) {
        alert('所有方向已分配完毕！');
        return;
      }
      const idx = Math.floor(Math.random() * directions.length);
      const selected = directions.splice(idx, 1)[0];

      $(this).find('.front').text(selected);
      $(this).addClass('flipped disabled');

      if(directions.length === 0){
        $('#resetBtn').prop('disabled', false);
      }
    });

    $('#resetBtn').on('click', function(){
      resetGame();
    });

    resetGame();
  });
</script>

</body>
</html>